package com.btprice.plugin;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class SelectOnePlugin extends PluginAdapter {

    private Logger log = LoggerFactory.getLogger("selectoneplugin");

    public static final String METHOD_NAME = "selectOneByExample";  // 方法名

    @Override
    public boolean validate(List<String> list) {
        if (StringUtility.stringHasValue(getContext().getTargetRuntime()) && "MyBatis3".equalsIgnoreCase(getContext().getTargetRuntime()) == false ){
            log.warn("itfsw:插件"+this.getClass().getTypeName()+"要求运行targetRuntime必须为MyBatis3！");
            return false;
        }
        return true;
    }


    /**
     * 生成完成后会调用此方法
     * @param interfaze
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
        Method selectOneMethod = new Method(METHOD_NAME);
        selectOneMethod.setAbstract(true);
        FullyQualifiedJavaType returnType = introspectedTable.getRules().calculateAllFieldsClass();
        selectOneMethod.setReturnType(returnType);
        FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
        selectOneMethod.addParameter(new Parameter(type, "example"));
        interfaze.addMethod(selectOneMethod);
        return true;
    }


    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        //数据库表名
        String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
        // 生成查询语句
        XmlElement selectOneElement = new XmlElement("select");

        // 添加ID
        selectOneElement.addAttribute(new Attribute("id", METHOD_NAME));

        // 添加返回类型
        selectOneElement.addAttribute(new Attribute("resultMap", introspectedTable.getBaseResultMapId()));
        // 添加参数类型
        selectOneElement.addAttribute(new Attribute("parameterType", introspectedTable.getExampleType()));
        selectOneElement.addElement(new TextElement("select")); //$NON-NLS-1$
        selectOneElement.addElement(getBaseColumnListElement(introspectedTable));
        StringBuilder sb = new StringBuilder();
        sb.append("from "); //$NON-NLS-1$
        sb.append(tableName);
        selectOneElement.addElement(new TextElement(sb.toString()));

        selectOneElement.addElement(getExampleIncludeElement(introspectedTable));

        // 只查询一条
        selectOneElement.addElement(new TextElement("limit 1"));

        document.getRootElement().addElement(selectOneElement);

        return true;

    }


    protected XmlElement getBaseColumnListElement(IntrospectedTable introspectedTable) {
        XmlElement answer = new XmlElement("include"); //$NON-NLS-1$
        answer.addAttribute(new Attribute("refid", //$NON-NLS-1$
                introspectedTable.getBaseColumnListId()));
        return answer;
    }


    protected XmlElement getExampleIncludeElement(IntrospectedTable introspectedTable) {
        XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
        ifElement.addAttribute(new Attribute("test", "_parameter != null")); //$NON-NLS-1$ //$NON-NLS-2$

        XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
        includeElement.addAttribute(new Attribute("refid", //$NON-NLS-1$
                introspectedTable.getExampleWhereClauseId()));
        ifElement.addElement(includeElement);

        return ifElement;
    }
}
